package com.meilai.project.controller.web.report.dto;

import com.meilai.project.constant.DateRangeEnum;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.temporal.TemporalAdjusters;

/**
 * @author xchen
 * @since 2022-04-12
 */
@Data
@ApiModel
public class DrawRateDetailDTO {
	@ApiModelProperty("日期类型")
	DateRangeEnum dateRangeType;
	@ApiModelProperty("开始日期(不用传，根据日期类型自动计算)")
	private LocalDate start_date;
	@ApiModelProperty("结束日期(不用传，根据日期类型自动计算)")
	private LocalDate end_date;

	public void initDateRange() {
		if (DateRangeEnum.MONTH.equals(dateRangeType)) {
			LocalDate startOfMonth = LocalDate.now().withDayOfMonth(1);
			this.start_date = startOfMonth.minusMonths(1);
			this.end_date = startOfMonth.minusDays(1);
		} else if(DateRangeEnum.YEAR.equals(dateRangeType)){
			LocalDate nowDate  = LocalDate.now();
			this.start_date = nowDate.with(TemporalAdjusters.firstDayOfYear());
			this.end_date = nowDate.with(TemporalAdjusters.lastDayOfYear());
		} else {
			this.start_date = LocalDate.now().with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY));
			this.end_date = LocalDate.now().with(TemporalAdjusters.nextOrSame(DayOfWeek.SUNDAY));
		}
	}

}
